package com.leyou.cart.config;

import com.leyou.auth.utils.RsaUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;

import javax.annotation.PostConstruct;
import java.io.File;
import java.security.PrivateKey;
import java.security.PublicKey;

/**
 * @author 岩冰
 * @version 1.0
 * @date:2018/10/18
 */
@ConfigurationProperties(prefix = "ly.jwt")
public class JwtProperties {

    private String pubKeyPath;
    private PublicKey publicKey; // 公钥
    private String cookieName;

    public PublicKey getPublicKey() {
        return publicKey;
    }

    public JwtProperties setPublicKey(PublicKey publicKey) {
        this.publicKey = publicKey;
        return this;
    }
    public String getCookieName() {
        return cookieName;
    }

    public JwtProperties setCookieName(String cookieName) {
        this.cookieName = cookieName;
        return this;
    }

    private static final Logger logger = LoggerFactory.getLogger(JwtProperties.class);
    @PostConstruct
    public void init() {
        try {
            this.publicKey = RsaUtils.getPublicKey(pubKeyPath);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("初始化创建公错误",e);
            throw new RuntimeException();
        }

    }


    public String getPubKeyPath() {
        return pubKeyPath;
    }

    public JwtProperties setPubKeyPath(String pubKeyPath) {
        this.pubKeyPath = pubKeyPath;
        return this;
    }

}
